<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Servicehistory_model extends CI_Model {

	function __construct() {
		parent::__construct();
		
	}

	/**
	 * 회원의 운세 이용 내역
	 */
	function select_service_history_list($user_id, $page_no, $page_size) {
		log_message('debug',"#### Servicehistory_model::select_service_history_list($user_id, $page_no, $page_size)");
		
		$start_row = ($page_no-1) * $page_size;

		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       S.idx ";
		$sql .= PHP_EOL."     , S.user_id ";
		$sql .= PHP_EOL."     , S.cash ";
		$sql .= PHP_EOL."     , S.point ";
		$sql .= PHP_EOL."     , (SELECT service_name FROM tb_service WHERE service_id = S.service_id) service_name ";
		$sql .= PHP_EOL."     , S.result_html_url ";
		$sql .= PHP_EOL."     , S.create_dt ";
		$sql .= PHP_EOL." FROM  tb_service_used_history S ";
		$sql .= PHP_EOL." WHERE S.user_id = ? ";
		$sql .= PHP_EOL." AND   S.secret_yn = 'N' ";
		$sql .= PHP_EOL." ORDER BY S.create_dt DESC ";
		$sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	function update_secret_yn($idx, $user_id, $secret_yn) {
		log_message('debug',"#### Servicehistory_model::update_secret_yn($idx, $user_id, $secret_yn)");
		
		$this->db->set('secret_yn',$secret_yn);
		$this->db->where('idx'    ,$idx);
		$this->db->where('user_id',$user_id);
		$this->db->update('tb_service_used_history') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}

	/**
	 * 총 내역 수
	 */
	function get_service_history_total_count($user_id) {
		$this->db->where('user_id', $user_id);
		$this->db->where('secret_yn', 'N');
		$this->db->from('tb_service_used_history');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 회원의 운세 이용 내역 (패키지 쿠폰)
	 */
	function select_package_history_list($user_id, $package_idx, $page_no, $page_size) {
		log_message('debug',"#### Servicehistory_model::select_package_history_list($user_id, $package_idx, $page_no, $page_size)");
		
		$start_row = ($page_no-1) * $page_size;

		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       S.user_id ";
		$sql .= PHP_EOL."     , (SELECT service_group_name FROM tb_service_group WHERE service_group_id = S.coupon_id) coupon_name";
		$sql .= PHP_EOL."     , (SELECT service_name FROM tb_service WHERE service_id = S.service_id) service_name ";
		$sql .= PHP_EOL."     , S.create_dt ";
		$sql .= PHP_EOL." FROM  tb_service_used_history S ";
		$sql .= PHP_EOL." WHERE S.user_id = ? ";
		$sql .= PHP_EOL." AND   S.package_idx = ? ";
		$sql .= PHP_EOL." ORDER BY S.create_dt DESC ";
		$sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		$query = $this->db->query($sql, array($user_id, $package_idx)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 총 내역 수
	 */
	function get_package_history_total_count($user_id, $package_idx) {
		log_message('debug',"#### Servicehistory_model::get_package_history_total_count($user_id, $package_idx)");
		
		$this->db->where('user_id', $user_id);
		$this->db->where('package_idx', $package_idx);
		$this->db->from('tb_service_used_history');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

}

/* End of file servicehisotory_model.php */
/* Location: ./application/models/mypage/servicehisotory_model.php */